<template>
	<view class="order-detail-page">
		<uv-skeleton
			rows="4"
			:loading="loading"
			:title="false"
			:rowsWidth="['100%', '25%', '35%', '60%']"
			:rowsHeight="['240rpx', '40rpx', '36rpx', '36rpx']"
		>
			<view class="order-base-data">
				<hsb-business-order-card
					:orderInfo="orderInfo"
					@deliver="updateExpress"
				/>
				<hsb-business-order-recycle-way
					:orderInfo="orderInfo"
					:routeList="routeList"
				/>
			</view>
		</uv-skeleton>
		<uv-skeleton
			rows="1"
			:loading="loading"
			:title="false"
			:rowsWidth="['100%']"
			:rowsHeight="['250rpx']"
		>
			<view class="order-attention-data">
				<hsb-business-order-attention
					:recycleType="orderInfo.recycleType"
				/>
			</view>
		</uv-skeleton>
		<uv-skeleton
			rows="6"
			:loading="loading"
			:title="false"
			:rowsWidth="['20%', '25%', '35%', '100%', '100%', '100%']"
			:rowsHeight="['40rpx', '40rpx', '40rpx', '40rpx', '40rpx', '40rpx']"
		>
			<view class="order-product-data">
				<hsb-business-order-product
					:orderInfo="orderInfo"
					:reportInfo="reportInfo"
				/>
			</view>
			<view class="order-basics-data">
				<hsb-business-order-basics
					:orderInfo="orderInfo"
				/>
			</view>
		</uv-skeleton>
		<hsb-business-order-operate
			v-if="!loading"
			:orderInfo="orderInfo"
			@cancel="confirmCancel"
			@agree="confirmRecycle"
			@again="recycleAgain"
			@service="$store.dispatch('contactService')"
			@revaluation="revaluation"
		/>
	</view>
</template>

<script>

export default {
	data () {
		return {
			orderNum: '',
			loading: true,
			recycling: false,
			orderInfo: {},
			reportInfo: {},
			routeList: []
		}
	},
	methods: {
		/*
		 * 用户订单信息
		*/
		async getOrderDetail () {
			const [res] = await this.$apis.getOrderDetail({
				orderNum: this.orderNum
			})
			if (res._errCode === '0') {
				const orderInfo = res._data || {}
				orderInfo.orderStatusIcon = orderInfo.orderStatusIcon.replace(/s1-1251010403.file.myqcloud.com/, 's1.huishoubao.com')
				if (orderInfo.lastPrice < 0) {
					orderInfo.negative = true
					orderInfo.lastPriceNum = Math.abs(orderInfo.lastPrice)
				}
				this.orderInfo = orderInfo
				/**
				 * 质检报告
				 */
				if (orderInfo.evaluateOptions.length || orderInfo.detectOptions.length) {
					const warnList = []
					const normalList = []
					const { reportUrl, recycleOldProduct, recycleNewProduct, otherDiffOptions } = orderInfo
					const isSameModel = recycleOldProduct === recycleNewProduct
					orderInfo.evaluateOptions.forEach(item => {
						if (item.diff === '1') {
							warnList.push(item)
						} else {
							normalList.push(item)
						}
					})
					this.reportInfo = {
						reportUrl,
						recycleOldProduct,
						recycleNewProduct,
						otherDiffOptions,
						isSameModel,
						detectList: isSameModel ? warnList.concat(normalList) : orderInfo.detectOptions
					}
				}
			} else {
				uni.showToast({
					title: res._errStr || '获取订单信息失败',
					icon: 'none'
				})
			}
			this.loading = false
		},
		/*
		 * 取消订单
		*/
		confirmCancel () {
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 0
			})
			uni.navigateTo({
				url: `/pages/order/cancel/index?orderNum=${this.orderInfo.orderNum}`
			})
		},
		/*
		 * 确认回收
		*/
		async confirmRecycle () {
			if (this.recycling) return
			this.recycling = true
			uni.showLoading({
				title: '提交中',
				mask: true
			})
			const [res] = await this.$apis.agreeRecycle({
				orderNum: this.orderInfo.orderNum
			})
			this.recycling = false
			uni.hideLoading()
			this.getOrderDetail()
			if (res._errCode === '0') {
				uni.showToast({
					title: '确认成功',
					icon: 'success'
				})
			} else {
				uni.showToast({
					title: res._errStr || '确认失败',
					icon: 'none'
				})
			}
		},
		/*
		 * 更新快递单号
		*/
		updateExpress () {
			const { orderNum, code = '', expressCompany = '' } = this.orderInfo
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 0
			})
			uni.navigateTo({
				url: `/pages/order/deliver/index?orderNum=${orderNum}&expressCode=${code}&expressName=${expressCompany}`
			})
		},
		/*
		 * 再下一单
		*/
		recycleAgain () {
			uni.navigateTo({
				url: '/pages/products/index'
			})
		},
		/*
		 * 重新估价
		*/
		revaluation () {
			if (!this.orderInfo.productId) {
				return this.recycleAgain()
			}
			uni.navigateTo({
				url: `/pages/evaluate/index?productId=${this.orderInfo.productId}`
			})
		},
	},
	onLoad (options) {
		this.orderNum = options.orderNum || ''
	},
	onShow () {
		this.getOrderDetail()
	}
}
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
